/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.web.exercisehistory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.web.BaseController;
import com.idenger.common.utils.StringUtils;
import com.idenger.modules.edu.entity.exercisehistory.EduExerciseHistory;
import com.idenger.modules.edu.service.exercisehistory.EduExerciseHistoryService;

/**
 * 用户做练习记录Controller
 * @author 兰正军
 * @version 2017-12-04
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/exercisehistory/eduExerciseHistory")
public class EduExerciseHistoryController extends BaseController {

	@Autowired
	private EduExerciseHistoryService eduExerciseHistoryService;
	
	@ModelAttribute
	public EduExerciseHistory get(@RequestParam(required=false) String id) {
		EduExerciseHistory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduExerciseHistoryService.get(id);
		}
		if (entity == null){
			entity = new EduExerciseHistory();
		}
		return entity;
	}
	
	@RequiresPermissions("edu:exercisehistory:eduExerciseHistory:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduExerciseHistory eduExerciseHistory, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EduExerciseHistory> page = eduExerciseHistoryService.findPage(new Page<EduExerciseHistory>(request, response), eduExerciseHistory); 
		model.addAttribute("page", page);
		return "modules/edu/exercisehistory/eduExerciseHistoryList";
	}

	@RequiresPermissions("edu:exercisehistory:eduExerciseHistory:view")
	@RequestMapping(value = "form")
	public String form(EduExerciseHistory eduExerciseHistory, Model model) {
		model.addAttribute("eduExerciseHistory", eduExerciseHistory);
		return "modules/edu/exercisehistory/eduExerciseHistoryForm";
	}

	@RequiresPermissions("edu:exercisehistory:eduExerciseHistory:edit")
	@RequestMapping(value = "save")
	public String save(EduExerciseHistory eduExerciseHistory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eduExerciseHistory)){
			return form(eduExerciseHistory, model);
		}
		eduExerciseHistoryService.save(eduExerciseHistory);
		addMessage(redirectAttributes, "保存用户做练习记录成功");
		return "redirect:"+Global.getAdminPath()+"/edu/exercisehistory/eduExerciseHistory/?repage";
	}
	
	@RequiresPermissions("edu:exercisehistory:eduExerciseHistory:edit")
	@RequestMapping(value = "delete")
	public String delete(EduExerciseHistory eduExerciseHistory, RedirectAttributes redirectAttributes) {
		eduExerciseHistoryService.delete(eduExerciseHistory);
		addMessage(redirectAttributes, "删除用户做练习记录成功");
		return "redirect:"+Global.getAdminPath()+"/edu/exercisehistory/eduExerciseHistory/?repage";
	}

}